<?php


namespace app\api\controller\v1;

use app\admin\curd\water_ele_price\WaterElePriceRead;
use app\api\controller\BaseController;
use app\logic\RoomLeaseRecordLogic;
use app\logic\UserLogic;
use app\logic\PayRecordLogic;
use app\logic\RepairLogic;
use app\logic\RoomLogic;
use app\model\Building;
use app\model\WaterEleRecord;
use app\exception\MessageException;
use app\validate\WaterEleRecordValidate;
use app\model\RepairUser;
use app\model\RoomLeaseRecord;
class Room extends BaseController
{
    protected $name = '房间';

    protected $methodName = [
        'addCkeckRecord'        => '申请入住',
        'getCkeckRecord'        => '获取申请记录',
        'getUserRecord'         => '获取缴费记录',
        'getRecordList'         => '获取用户全部缴费记录',
        'addRepairRecord'       => '添加报修记录',
        'getRepairReocrd'       => '获取报修记录',
        'getUserRoom'           => '获取用户的房间号',
        'saveRepair'            => '修改报修记录',
        'getBuilding'           => '获取楼房号',
        'getRoomList'           => '获取房间列表',
        'getWaterEle'           => '获取上次水电表读数',
        'addWaterEleRecord'     => '添加水电读数',
        'get_water_ele_list'     => '获取水电记录'
    ];

    /**
     * @Description: 申请入住记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-08 22:28:40
     * @version: 1.0.0
     * @return [type]
     */    
    public function addCheckRecord(){

        $params = $this->request->post();

        $where['id'] = $params['user_id'];
        $data = [
            'real_name' => $params['real_name'],
            'word_unit' => $params['word_unit']
        ];
        $save = UserLogic::saveUser($where, $data);

        $reocrdWhere[] =  ['user_id','=',$params['user_id']];
        $reocrdWhere[] =  ['status', 'in', '0,1'];

        $reocrd = RoomLeaseRecord::where($reocrdWhere)->find();

        if($reocrd){
            throw new MessageException('您已提交过申请了');
        }

        $res = RoomLeaseRecordLogic::addRecord($params);
        if($res){
            return success($res, '获取成功');
        }else{
            return error('申请失败');
        }
    }

    /**
     * @Description: 获取申请记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-09 10:15:50
     * @version: 1.0.0
     * @return [type]
     */    
    public function getCheckRecord(){

        $params = $this->request->get();

        $where['user_id'] = $params['user_id'];

        $res = RoomLeaseRecordLogic::getRecord($where);

        return success($res, '获取成功');
        
    }
    
    /**
     * @Description: 获取用户缴费详情
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-10 13:48:17
     * @version: 1.0.0
     * @return [type]
     */    
    public function getUserRecord(){

        $params = $this->request->get();

        $res = PayRecordLogic::getUserRecord($params);

        return success($res,'获取成功');
    }

    /**
     * @Description: 获取用户全部缴费记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-10 15:54:17
     * @version: 1.0.0
     * @return [type]
     */   
    public function getRecordList(){
        $params = $this->request->get();
        $res = PayRecordLogic::getUserRecordList($params);

        return success($res, '获取成功');
    }

    /**
     * @Description: 添加报修记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-11 09:48:23
     * @version: 1.0.0
     * @return [type]
     */    
    public function addRepairRecord(){
        $params = $this->request->post();

        $res = RepairLogic::addRepairRecord($params);

        return success($res,'添加成功');
        
    }

    /**
     * @Description: 获取报修记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-14 11:31:02
     * @version: 1.0.0
     * @return [type]
     */
    public function getRepairReocrd(){
        $params = $this->request->get();

        $res = RepairLogic::getRepairReocrd($params);
        return success($res,'获取成功');
    }

    /**
     * @Description: 获取用户居住房间号
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-14 11:31:46
     * @version: 1.0.0
     * @return [type]
     */
    public function getUserRoom(){
        $params = $this->request->get();

        $where['user_id'] = $params['user_id'];
        $where['status']  = 1;

        $res = RoomLeaseRecordLogic::getRecord($where);
        $result = [];
        if(count($res)>=1){
            $roomWhere['id'] = $res[0]['room_id'];
            $result = RoomLogic::getRoom($roomWhere);
        }

        return success($result, '获取成功');
    }

    /**
     * @Description: 获取报修详情
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-19 16:39:40
     * @version: 1.0.0
     * @return [type]
     */    
    public function getRepairDetail(){
        $params = $this->request->get();

        $res = RepairLogic::getRepairDetail($params);

        return  success($res,'获取成功');
    }

    /**
     * @Description: 修改报修记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-21 10:33:01
     * @version: 1.0.0
     * @return [type]
     */    
    public function saveRepair(){
        $params = $this->request->post();
        
        $res = RepairLogic::saveRepair($params);

        return success($res,'修改成功');
    }

    /**
     * @Description: 获取楼号
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-21 17:27:47
     * @version: 1.0.0
     * @return [type]
     */    
    public function getBuilding(){

        $res = Building::getBuildingClient();
        if($res){
            foreach ($res as $key => $value) {
                $res[$key]['building_name'] = $value['name'].$value['building_number'];
            }
        }
        return success($res, '获取成功');
    }

    /**
     * @Description: 获取房间列表
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-22 09:17:54
     * @version: 1.0.0
     * @return [type]
     */    
    public function getRoomList(){

        $params = $this->request->get();
        $res = RoomLogic::getRoomList($params);
        return success($res,'获取成功');
    }

    /**
     * @Description: 获取上次水电表读数
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-22 11:14:51
     * @version: 1.0.0
     * @return [type]
     */    
    public function getWaterEle(){

        $params = $this->request->get();
        $where['room_id'] = $params['room_id'];
        $res = WaterEleRecord::getWaterEle($where);

        if(!$res){
            return success([],'暂无记录');
        }
        return success($res,'获取成功');
    }

    /**
     * @Description: 添加水电记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-22 15:46:43
     * @version: 1.0.0
     * @return [type]
     */    
    public function addWaterEleRecord(){
        $params = $this->request->post();
        WaterEleRecordValidate::batchCheck('create');
        $params = WaterEleRecordValidate::getDataByRule('create');
        $params['record_date'] = $params['record_date']/1000;
        $month = date("Y-m", $params['record_date']);

        $where['id'] = $params['user_id'];
        $where['status'] = 1;

        $userInfo = RepairUser::where($where)->find();
        if (!$userInfo) {
            // throw new MessageException('该房间' . $month . '已生成缴费记录');
            return success([], '您的维护员账号已被禁用');
        }
        
        //指定月份第一天的时间戳
        $lastMonthfirstDay = strtotime(date("Y-m-d 00:0:00", strtotime("first day of {$month}")));

        // 指定月份最后一天的时间戳
        $lastMonthLastDay = strtotime(date("Y-m-d 23:59:59", strtotime("{$month} +1 month -1 day")));

        $findWhere[]   = ['record_date', 'between', "$lastMonthfirstDay, $lastMonthLastDay", 'AND'];
        $findWhere[]   = ['room_id', '=', $params['room_id']];

        $findRecord = WaterEleRecord::getWaterEle($findWhere);
        if ($findRecord) {
            // throw new MessageException('该房间' . $month . '已生成缴费记录');
            return success([], '该房间' . $month . '已生成缴费记录');
        }
        $res = WaterEleRecord::create($params);
        // print_r($params);

        return success($res, '录入成功');

    }

    /**
     * @Description: 获取水电记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-22 17:11:03
     * @version: 1.0.0
     * @return [type]
     */    
    public function get_water_ele_list(){
        $params = $this->request->get();

        $res = WaterEleRecord::getWaterEleList();

        if($res){
            foreach ($res as $key => $value) {
                $res[$key]['record_date'] = date("Y-m-d", $value['record_date']);
            }
        }

        return success($res,'获取成功');
    }
    
}